import numpy, cv2
from PIL import Image, ImageDraw, ImageFont
from app1 import views
from app2 import detect_mask

detector = detect_mask.Detector()


def detect(image):
    # print('a'+detector)
    res = detector.detect(source=views.img_path)
    print(res)
    for item in res:
        if (item[0] == 'mask'):
            color = (0, 255, 255)
            rbg = (255, 255, 0)
            text = '普通口罩'
        else:
            color = (0, 255, 0)
            rbg = (0, 255, 0)
            text = 'N95口罩'
        cv2.rectangle(image, (int(item[1][0]), int(item[1][1])), (int(item[1][2]), int(item[1][3])), color, 5)
        # 添加文字
        img = Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
        # cv2.show(img)
        draw = ImageDraw.Draw(img)
        font = ImageFont.truetype('simhei.ttf', 30, encoding="utf-8")
        draw.text((int(item[1][0]), int(item[1][1]) - 35), text, rbg, font=font)

        back = cv2.cvtColor(numpy.asarray(img), cv2.COLOR_RGB2BGR)

        return back
    return 'none'
